//
//  FYPicPikerCollectionView.swift
//  dugeweibo
//
//  Created by pelesys on 16/8/7.
//  Copyright © 2016年 杜方宜. All rights reserved.
//

import UIKit

private let picPickerCell = "picPickerCell"
private let edgeMargin : CGFloat = 15
class FYPicPikerCollectionView: UICollectionView {
    // MARK:-定义属性
    var images : [UIImage] = [UIImage](){
        didSet {
            reloadData()
        }
    }
   // MARK:-系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置collectionView的layout
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let itemH = (UIScreen.main.bounds.width - 4 * edgeMargin) / 3
        layout.itemSize = CGSize(width: itemH, height: itemH)
        layout.minimumInteritemSpacing = edgeMargin
        layout.minimumLineSpacing = edgeMargin
        //设置colectionView的属性
        register(UINib(nibName: "FYPicPikerCollectionViewCell",bundle: nil), forCellWithReuseIdentifier: picPickerCell)
        dataSource = self
        //设置collection的内边距
        contentInset = UIEdgeInsets(top: edgeMargin, left: edgeMargin, bottom: 0, right: edgeMargin)
        
    }
}
// MARK:-UICollectionView代理方法
extension FYPicPikerCollectionView : UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //1.创建cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: picPickerCell, for: indexPath as IndexPath) as! FYPicPikerCollectionViewCell
        //2.给cell设置数据
        cell.backgroundColor = UIColor.red
        cell.image = indexPath.item <= images.count - 1 ?images[indexPath.item] : nil

        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count + 1
    }
    
}
